package com.youchuang.dao.impl;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.annotation.Resource;

import org.apache.commons.collections.CollectionUtils;
import org.springframework.stereotype.Repository;

import com.youchuang.bean.ReceiveAddress;
import com.youchuang.common.HibernateConnection;
import com.youchuang.dao.ReceiveAddressDAO;

@Repository("receiveAddressDAO")
public class ReceiveAddressDAOImpl implements ReceiveAddressDAO {

	private static final String HQL_FIND_BY_USER_ID = "from ReceiveAddress where userId = :userId";
	private static final String HQL_FIND_BY_ID = "from ReceiveAddress where id = :id";

	@Resource
	private HibernateConnection<ReceiveAddress> hibernateConnection;

	@Override
	public void save(ReceiveAddress t) {
		hibernateConnection.save(t);
	}

	@Override
	public void delete(ReceiveAddress t) {
		hibernateConnection.delete(t);
	}

	@Override
	public int update(ReceiveAddress t) {
		hibernateConnection.update(t);
		return 1;
	}

	@Override
	public List<ReceiveAddress> queryAll() {
		// Do nothing because of it unusual
		return null;
	}

	@Override
	public List<ReceiveAddress> queryByUserId(String userId) {
		Map<String, Object> params = new HashMap<>();
		params.put("userId", userId);
		return hibernateConnection.find(HQL_FIND_BY_USER_ID, params);
	}

	@Override
	public ReceiveAddress queryById(String id) {
		Map<String, Object> params = new HashMap<>();
		params.put("id", id);
		List<ReceiveAddress> list = hibernateConnection.find(HQL_FIND_BY_ID, params);
		if (CollectionUtils.isEmpty(list)) {
			return null;
		}
		return list.get(0);
	}

}
